package indi.mumu.common.controller;

import java.math.BigDecimal;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import indi.mumu.common.model.Clothes;
import indi.mumu.common.model.Saleorder;
import indi.mumu.common.model.Saleorderitem;
import indi.mumu.service.SaleOrderItemService;
import indi.mumu.validator.POSValidator;

public class SaleOrderItemController extends Controller{

	private SaleOrderItemService itemService = new SaleOrderItemService();
	
	public void index() {
		String saleOrderId = getPara("SaleOrder.saleOrderId");
		setAttr("saleOrderItemPage", Saleorder.dao.paginate(getParaToInt(0, 1), 10, "select *", "from saleorder where saleOrder_id", saleOrderId));
		render("sell.html");
	}
	
	/**

	 * 添加订单详情

	 */
	public void add(){
		Saleorderitem saleItem = getModel(Saleorderitem.class);
		itemService.addItem(saleItem);
	}
	/**

	 * 通过订单号获取订单详情

	 */
	public void getItemById(){
		String saleOrderId = getPara("SaleOrder.saleOrderId");
		itemService.getAimItem(saleOrderId);
		render("saleorderitem.html");
	}
	
	public void edit() {
		setAttr("saleorderitem", Saleorderitem.dao.findById(getPara("id")));
	}
	
	public void update() {
		getModel(Saleorderitem.class).update();
		String saleOrderId = getPara("saleOrderId");
		setAttr("saleOrderItemPage", Saleorderitem.dao.paginate(getParaToInt(0, 1), 10, "select *","from saleorderitem where saleOrder_id=" + saleOrderId));
		render("/sell/saleorderitem.html");
	}
	
	public void delete() {
		Saleorderitem.dao.deleteById(getPara("id"));
		String saleOrderId = getPara("saleOrderId");
		List<Clothes> price = Clothes.dao.find("select * from clothes where product_id="+getPara("productId"));
		BigDecimal total = price.get(0).getPrice().multiply(new BigDecimal(getPara("count")));
		String sql = "UPDATE saleorder SET Total = Total - " + price + " WHERE saleOrder_id = " + saleOrderId;
		Db.update(sql);
		setAttr("saleOrderItemPage", Saleorderitem.dao.paginate(getParaToInt(0, 1), 10, "select *","from saleorderitem where saleOrder_id=" + saleOrderId));
		render("/sell/saleorderitem.html");
	}
}
